
package com.hlkj.pay.controller.openapi.order;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.pay.vo.openapi.order.req.OpenApiOrderTransferReq;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.openapi.IOpenApiAppService;
import com.hlkj.pay.app.order.ITransferOrderAppService;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.vo.openapi.order.req.OpenApiOrderQueryReq;
import com.hlkj.pay.vo.openapi.order.req.OpenApiOrderTransferPreReq;
import com.hlkj.pay.vo.openapi.order.resp.OpenApTransferBalanceResp;
import com.hlkj.pay.vo.openapi.order.resp.OpenApiOrderTransferResp;
import com.hlkj.pay.vo.order.OpenApiResult;
import com.hlkj.pay.vo.order.req.OrderCommonParamReq;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/09 11:25
 */
@Tag(name = "汇电通-转账管理")
@RestController
@RequestMapping("/openapi/transfer")
@RequiredArgsConstructor
@Slf4j
public class OpenApiOrderTransferController {

    private final IOpenApiAppService openApiAppService;

    private final ITransferOrderAppService transferOrderAppService;

    @Operation(summary = "账户余额查询")
    @PostMapping("/balance")
    OpenApiResult<OpenApTransferBalanceResp> balance(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            OpenApiOrderQueryReq orderQueryReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), OpenApiOrderQueryReq.class);
            CommonResult<OpenApTransferBalanceResp> stringOpenApiResult = transferOrderAppService.balanceQuery(orderQueryReq.getMchChannelCode());
            return openApiAppService.sign(stringOpenApiResult);
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "提交转账预订单")
    @PostMapping("/create/pre")
    OpenApiResult<OpenApiOrderTransferResp> submitPreOrder(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            OpenApiOrderTransferPreReq orderTransferReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), OpenApiOrderTransferPreReq.class);
            CommonResult<OpenApiOrderTransferResp> stringOpenApiResult = transferOrderAppService.submitPreOrder(orderTransferReq);
            return openApiAppService.sign(stringOpenApiResult);
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "提交转账订单")
    @PostMapping("/create")
    OpenApiResult<OpenApiOrderTransferResp> submitOrder(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            OpenApiOrderTransferReq orderTransferReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), OpenApiOrderTransferReq.class);
            CommonResult<OpenApiOrderTransferResp> stringOpenApiResult = transferOrderAppService.submitOrder(orderTransferReq);
            return openApiAppService.sign(stringOpenApiResult);
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "转账订单查询")
    @PostMapping("/query")
    OpenApiResult<OpenApiOrderTransferResp> queryOrder(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            OpenApiOrderQueryReq accessQueryReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), OpenApiOrderQueryReq.class);
            CommonResult<OpenApiOrderTransferResp> stringOpenApiResult = transferOrderAppService.queryOrder(accessQueryReq);
            return openApiAppService.sign(stringOpenApiResult);
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

}
